package com.nnfe.fyd.feature.inquiry;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nnfe.fyd.BuildConfig;
import com.nnfe.fyd.BusKey;
import com.nnfe.fyd.R;
import com.nnfe.fyd.base.ActionBarFragment;
import com.nnfe.fyd.bean.myinquirt.InquirtBean;
import com.nnfe.fyd.feature.inquiryreport.InquiryReportAcivity;
import com.nnfe.fyd.utils.ImageUtil;
import com.nnfe.fyd.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.math.BigDecimal;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by dada on 2017/3/21.
 * 询价详情
 */

public class MyInquiryDetailFragment extends ActionBarFragment {
    @Bind(R.id.xunjiafangshi)
    TextView xunjiafangshi;
    @Bind(R.id.xiaoqumingcheng)
    TextView xiaoqumingcheng;
    @Bind(R.id.fangwudizhi)
    TextView fangwudizhi;
    @Bind(R.id.jianzhumianji)
    TextView jianzhumianji;
    @Bind(R.id.guihuayongtu)
    TextView guihuayongtu;
    @Bind(R.id.diyaqingkuang)
    TextView diyaqingkuang;
    @Bind(R.id.yidiyuer)
    TextView yidiyuer;
    @Bind(R.id.fangbenzhaopian)
    TextView fangbenzhaopian;
    @Bind(R.id.zhaopian)
    ImageView zhaopian;
    @Bind(R.id.daikuanedu)
    TextView daikuanedu;
    @Bind(R.id.dingdanhao)
    TextView dingdanhao;

    InquirtBean.Inquirt bean;
    @Bind(R.id.baodan)
    Button baodan;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
        bean = (InquirtBean.Inquirt) getArguments().getSerializable("bean");

        dingdanhao.append(bean.id);


        LogUtil.e("dada", bean.areaName + " 1");
        LogUtil.e("dada", bean.housingArea + " 2");
        LogUtil.e("dada", bean.housingMortgage + " 3");
        LogUtil.e("dada", bean.id + " 4");
        LogUtil.e("dada", bean.isOwnership + " 5");
        LogUtil.e("dada", bean.loanAmount + " 6");
        LogUtil.e("dada", bean.loanLimitMin + " 7");
        LogUtil.e("dada", bean.loanLimitMax + " 7-1");
        LogUtil.e("dada", bean.mortgageAmount + " 8");
        LogUtil.e("dada", bean.picUrl4Test + " 9");
        LogUtil.e("dada", bean.picUrl4Work + " 0");
        LogUtil.e("dada", bean.roomAddress + " 11");


        // 有房本
        if (!TextUtils.isEmpty(bean.isOwnership)){
            LogUtil.e("xyd","有无房本？：" + bean.isOwnership);
            // 自动寻价是 无房本
        }
        if (bean.isOwnership.equalsIgnoreCase("1")) {
            xunjiafangshi.append("有房本");
            xiaoqumingcheng.append(bean.areaName);
            if (bean.housingMortgage.equalsIgnoreCase("2")) {
                if (!TextUtils.isEmpty(bean.mortgageAmount)) {
                    yidiyuer.append(bean.mortgageAmount + "万元");
                }
                diyaqingkuang.append("二抵");
            } else {
                diyaqingkuang.append("一抵");
                yidiyuer.setVisibility(View.GONE);
            }
            if (TextUtils.isEmpty(bean.loanLimitMax) || Double.parseDouble(bean.loanLimitMax) == 0) {
                daikuanedu.append("待反馈");
            } else {
                if (!TextUtils.isEmpty(bean.loanLimitMax) && !TextUtils.isEmpty(bean.loanLimitMin)) {
                    daikuanedu.append(new BigDecimal(bean.loanLimitMin).toPlainString());
                    daikuanedu.append("-");
                    daikuanedu.append(new BigDecimal(bean.loanLimitMax).toPlainString());
                    daikuanedu.append("万元");
                } else {
                    daikuanedu.append("待反馈");
                }

            }

            fangwudizhi.setVisibility(View.GONE);
            jianzhumianji.setVisibility(View.GONE);
            guihuayongtu.setVisibility(View.GONE);
            if (BuildConfig.DEBUG) {
                ImageUtil.displayImage(bean.picUrl4Test, zhaopian);
            } else {
                LogUtil.e("xyd", "release的图片地址是：" + bean.picUrl4Work);
                ImageUtil.displayImage(bean.picUrl4Work, zhaopian);
            }
        } else {
            // 无房本
            xunjiafangshi.append("无房本");
            xiaoqumingcheng.append(bean.areaName);

            if (!TextUtils.isEmpty(bean.roomAddress)){
                fangwudizhi.append(bean.roomAddress);
            }else {
                fangwudizhi.setVisibility(View.GONE);
            }

            jianzhumianji.append(bean.housingArea + "平米");
            guihuayongtu.append(bean.housingType);

            if (bean.housingMortgage.equalsIgnoreCase("2")) {
                if (!TextUtils.isEmpty(bean.mortgageAmount)) {
                    yidiyuer.append(bean.mortgageAmount + "万元");
                }
                diyaqingkuang.append("二抵");
            } else {
                diyaqingkuang.append("一抵");
                yidiyuer.setVisibility(View.GONE);
            }
            zhaopian.setVisibility(View.GONE);
            fangbenzhaopian.setVisibility(View.GONE);

            if (TextUtils.isEmpty(bean.loanLimitMax) || Double.parseDouble(bean.loanLimitMax) == 0) {
                daikuanedu.append("待反馈");
            } else {
                if (!TextUtils.isEmpty(bean.loanLimitMax) && !TextUtils.isEmpty(bean.loanLimitMin)) {
                    daikuanedu.append(new BigDecimal(bean.loanLimitMin).toPlainString());
                    daikuanedu.append("-");
                    daikuanedu.append(new BigDecimal(bean.loanLimitMax).toPlainString());
                    daikuanedu.append("万元");
                } else {
                    daikuanedu.append("待反馈");
                }

            }


        }

        // 是否可报单
        if (TextUtils.isEmpty(bean.loanLimitMax) || Double.parseDouble(bean.loanLimitMax) == 0) {
            baodan.setEnabled(false);
        } else {
            if (TextUtils.isEmpty(bean.loanAmount)) {
                baodan.setEnabled(true);
            } else {
                baodan.setEnabled(false);
                baodan.setText("已报单");
            }
        }

    }

    @Override
    public Object getTitleString() {
        return "询价详情";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_myinquiry_detail;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }

//

    @OnClick(R.id.baodan)
    public void onClick() {
        InquiryReportAcivity.lauch(mContext, bean.id, bean.loanLimit);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String str) {
        if (str.equalsIgnoreCase(BusKey.POST_inquiryReport)) {
            mContext.finish();
        }
    }
}
